<template>
  <div class="login-container">
    <div class="login-dialog">
      <div class="avatar-box">
        <img alt="头像" src="../assets/logo.png" />
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        class="login-form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="用户名"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            show-password
            @keydown.enter.native="submitForm"
          ></el-input>
        </el-form-item>
        <el-form-item class="button-box">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      登录组件
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  created() {
    if(process.env.NODE_ENV!=='production')
    {
      this.loginForm.username='admin';
      this.loginForm.password='123456'
    }
  },
  methods: {
    resetForm: function () {
      this.$refs.loginFormRef.resetFields();
    },
    submitForm: function () {
      this.$refs.loginFormRef.validate(async (result) => {
        if (result) {
          const { data: res } = await this.$http.post("login", this.loginForm);
          if (res.meta.status == 200) {
            this.$message.success("登录成功");
            window.sessionStorage.setItem("token", res.data.token);
            window.sessionStorage.setItem("user_id", res.data.id);
            this.$router.push("/home");
          } else {
            this.$message({
              type: "error",
              message: res.meta.msg,
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  color: white;
  height: 100%;
}
.login-dialog {
  background-color: white;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 00 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .login-form {
    padding: 0px 20px;
    position: absolute;
    width: 100%;
    bottom: 0px;
    box-sizing: border-box;
  }
  .button-box {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
